//   班级名称  jar包

const tools = (function () {

    const getStyle = (obj, attr) => {
        if (window.getComputedStyle) {
            return window.getComputedStyle(obj, null)[attr];
        }
        return obj.currentStyle[attr];
    }


    const animate = (oEl, json, callback) => {
        clearInterval(oEl.timer);
        oEl.timer = setInterval(() => {
            let current = 0;//当前
            let target = 0; //目标
            let steps = 0; //步长
            let flag = true;
            for (let attr in json) {
                if (attr == "opacity") {
                    target = parseFloat(json[attr]) * 100;
                    current = parseFloat(getStyle(oEl, attr)) * 100;
                } else {
                    target = parseInt(json[attr])
                    current = parseInt(getStyle(oEl, attr));
                }
                steps = (target - current) / 10;
                steps = steps > 0 ? Math.ceil(steps) : Math.floor(steps);
                if (attr == "opacity") {
                    oEl.style[attr] = (current + steps) / 100;
                } else if (attr == "zIndex") {
                    oEl.style[attr] = target;
                } else {
                    oEl.style[attr] = (current + steps) + 'px';
                }
                if (target != current) {
                    flag = false;
                }
            }

            if (flag) {
                clearInterval(oEl.timer);
                if (typeof (callback) == "function") {
                    callback();
                }
            }
        }, 20);
    }

    return {
        getStyle,
        animate
    }


})()
